package system.controller;

import common.entity.MyPageResult;
import common.rusult.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import system.entity.SysUser;
import system.service.SysUserService;

/**
 * @author wargod
 */
@RestController  //类中每个方法返回的对象会直接序列化成 JSON（默认用 Jackson）
@RequestMapping("/sysUser")//给当前类下的所有接口加一个公共访问路径前缀访问路径就是：http://localhost:8080/项目名/sysUser/XX
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    @PostMapping  //处理 POST 请求。
    //新增用户接口
    public JSONResult insertSysUser(@RequestBody SysUser sysUser){//从 请求体 JSON 反序列化为 SysUser 对象
        int result = sysUserService.insertSysUser(sysUser);
        return JSONResult .success(result);
    }

    //根据id删除用户接口
@DeleteMapping({"/{ids}"})//处理 DELETE 请求，并且 URL 中带 {ids} 变量
    public JSONResult deleteSysUser(@PathVariable int[] ids){
        int result = sysUserService.deleteSysUser(ids);
        return JSONResult.success(ids);
}

//修改用户接口
@PutMapping
    public JSONResult upddateSysUser(@RequestBody SysUser sysUser){
        sysUserService.updateSysUser(sysUser);
        return JSONResult.success();
}
//分页查询接口
    @GetMapping
    public JSONResult selectSysUser(SysUser sysUser) {
        MyPageResult<SysUser> list = sysUserService.selectSysUser(sysUser);
        return JSONResult.success(list);
    }
    //根据id查询用户接口
    @GetMapping("/selectSysUserId")
    public JSONResult selectSysUserById(@RequestParam("id") long id) {
        SysUser sysUser = sysUserService.selectSysUserById(id);
        return JSONResult.success(sysUser);
    }
}
